package com.hjq.fcloud.ui.adapter

import android.content.Context
import android.view.View
import android.widget.CheckBox
import android.widget.ListView
import android.widget.TextView
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.ProjectItemBean
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.other.MainDrawerListener
import com.zhy.adapter.abslistview.CommonAdapter
import com.zhy.adapter.abslistview.ViewHolder

/**
 * 抽屉-项目adapter
 */
class ProjectDrawerAdapter(
    context: Context,
    datas: MutableList<ProjectItemBean>?
) : CommonAdapter<ProjectItemBean>(
    context,
    R.layout.item_common_main_drawer,
    datas
) {

    var listener: MainDrawerListener? = null
    private var selectPos: Int = 0

    override fun convert(
        viewHolder: ViewHolder,
        item: ProjectItemBean,
        position: Int
    ) {
        var itemView = viewHolder.getView<View>(R.id.itemView)
        var titleTv = viewHolder.getView<TextView>(R.id.titleTv)
        titleTv.setText(item.project_name)

        var contentTv = viewHolder.getView<TextView>(R.id.contentTv)
        contentTv.setText(item.construct_corp_name)

        var checkBox = viewHolder.getView<CheckBox>(R.id.checkBox)
        checkBox.visibility = View.VISIBLE
        checkBox.isSelected = (selectPos != null && position == selectPos)

        viewHolder.getView<View>(R.id.redPointView).setVisibility(item.task_count > 0)

        itemView.setOnClickListener {
            selectPos = position
            notifyDataSetChanged()
            listener?.onSelect(position, item)
        }
    }

    fun setDrawerItemClickListener(listener: MainDrawerListener) {
        this.listener = listener
    }

    fun setSelection(lv: ListView, selectFnc:ProjectItemBean?) {
        for (pos in 0..mDatas?.size!! - 1) {
            var item = mDatas?.get(pos)
            if (item?.project_id.equals(selectFnc?.project_id)) {
                selectPos = pos
                break
            }
        }
        lv.setSelection(selectPos)
    }
}
